package com.nami.store.biz.user.application.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.nami.store.api.request.user.UserAddRequest;
import com.nami.store.api.response.user.UserDTO;
import com.nami.store.biz.user.application.UserAppService;
import com.nami.store.biz.user.convert.UserConvert;
import com.nami.store.biz.user.service.UserDomainService;
import com.nami.store.domain.UserDO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dromara.common.core.exception.user.UserException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserAppServiceImpl implements UserAppService {

    private final UserDomainService userDomainService;

    @Override
    public boolean saveUser(UserAddRequest request) {
        if (ObjectUtil.isNull(request)) {
            throw new UserException("找不到用户信息！");
        }

        userDomainService.saveUser(UserConvert.INSTANCE.convert2(request));
        return Boolean.TRUE;
    }

    @Override
    public boolean batchSave(List<UserAddRequest> users) {
        if (CollUtil.isEmpty(users)) {
            return false;
        }
        return userDomainService.batchSave(users);
    }

    @Override
    public UserDTO getUserByUserId(String userId) {
        if (StringUtils.isBlank(userId)) {
            return null;
        }
        return userDomainService.getUserByUserId(userId);
    }

    @Override
    public UserDTO getUserById(String appKey, String userId) {
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(appKey)) {
            return null;
        }
        return userDomainService.getUserById(appKey, userId);
    }

    @Override
    public List<UserDTO> getUserByIds(List<String> userIds) {
        if (CollUtil.isEmpty(userIds)) {
            return null;
        }
        return userDomainService.getUserByIds(userIds);
    }


    @Override
    public UserDTO getUserByMobile(String mobile) {
        if (StringUtils.isBlank(mobile)) {
            return null;
        }
        return userDomainService.getUserByMobile(mobile);
    }

    @Override
    public UserDO getUserByAccount(String account) {
        return null;
    }

    @Override
    public Map<String, UserDTO> getUsersByIds(List<String> userIds) {
        if (CollUtil.isEmpty(userIds)) {
            return new HashMap<>();
        }
        return userDomainService.getUsersByIds(userIds);
    }


}
